package org.mspring.mlog.web.module.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.util.Version;
import org.mspring.mlog.entity.News;
import org.mspring.mlog.entity.Post;
import org.mspring.mlog.search.Searchable;
import org.mspring.mlog.search.engine.SearchEngine;
import org.mspring.platform.dao.support.Page;
import org.mspring.platform.utils.StringUtils;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.wltea.analyzer.lucene.IKAnalyzer;

/**
 * 
 * @author Gao Youbo
 * @since 2013年7月5日
 */
@Widget
@RequestMapping("/search")
public class Web_SearchWidget extends Web_AbstractWidget {

    @Autowired
    private SearchEngine searchEngine;

    @SuppressWarnings({ "rawtypes" })
    @RequestMapping({ "/", "" })
    public String search(@ModelAttribute Page page, @RequestParam(required = false) String w, @RequestParam(required = false) String scope, HttpServletRequest request, HttpServletResponse response, Model model) {
        String[] fields = new String[] { "title", "summary", "content" };
        if (StringUtils.isNotBlank(w)) {
            w = StringUtils.encoding(w, "ISO-8859-1", "UTF-8").trim();
            try {
                BooleanQuery booleanQuery = new BooleanQuery();
                Class<? extends Searchable> clazz = null;
                if ("post".equals(scope)) {
                    clazz = Post.class;
                    QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_44, fields, new IKAnalyzer());
                    Query multiFieldQuery = queryParser.parse(w);

                    Term term = new Term("status", Post.Status.PUBLISH);
                    TermQuery termQuery = new TermQuery(term);

                    booleanQuery.add(multiFieldQuery, BooleanClause.Occur.MUST);
                    booleanQuery.add(termQuery, BooleanClause.Occur.MUST);
                } else {
                    clazz = News.class;
                    QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_44, fields, new IKAnalyzer());
                    Query multiFieldQuery = queryParser.parse(w);

                    Term term = new Term("status", News.Status.PUBLISH);
                    TermQuery termQuery = new TermQuery(term);

                    booleanQuery.add(multiFieldQuery, BooleanClause.Occur.MUST);
                    booleanQuery.add(termQuery, BooleanClause.Occur.MUST);
                }
                page = searchEngine.doSearch(clazz, booleanQuery, true, page);
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
        model.addAttribute("page", page);
        model.addAttribute("w", w);
        model.addAttribute("scope", scope);
        return "skin:/search/" + (scope == null ? "news" : scope);
    }
}
